package com.lk.ii.controller;

import com.github.pagehelper.PageHelper;
import com.lk.common.core.controller.BaseController;
import com.lk.common.core.page.TableDataInfo;
import com.lk.ii.domain.*;
import com.lk.ii.service.IIService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.SimpleDateFormat;
import java.util.*;

@Controller
public class ApiController extends BaseController {


    private static final SimpleDateFormat date = new SimpleDateFormat("yyyy-MM-dd");

    private static final SimpleDateFormat dateTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    @Autowired
    private IIService iiService;

    @GetMapping({"/api/news/{categoryId}/{page}"})
    @ResponseBody
    public TableDataInfo categoryArticleList(@PathVariable("categoryId") Long categoryId, @PathVariable("page") Long page) {
        Long pageSize = Long.valueOf(10);
        List<IiArticle> articleList = iiService.getCategoryArticleList(categoryId, pageSize,(page - 1) * pageSize);
        List<Map<String, Object>> list = new ArrayList<>();
        for (IiArticle article : articleList) {
            Map<String, Object> data = new HashMap<>();
            data.put("id", article.getId());
            data.put("cover", article.getCover());
            data.put("title", article.getTitle());
            IiAuthor author = iiService.getAuthorService().selectIiAuthorById(article.getAuthorId());
            data.put("author_name", author != null ? author.getName(): "");
            data.put("author_avatar", author != null ? author.getAvatar(): "");
            data.put("description", article.getDescription());
            data.put("publish_time", dateTime.format(article.getPublishTime()));
            list.add(data);
        }
        return getDataTable(list);
    }

    @GetMapping({"/api/video/{categoryId}/{page}"})
    @ResponseBody
    public TableDataInfo categoryVideoList(@PathVariable("categoryId") Long categoryId, @PathVariable("page") Long page) {
        Long pageSize = Long.valueOf(12);
        List<IiVideo> videoList = iiService.getCategoryVideoList(categoryId, pageSize,(page - 1) * pageSize);
        List<Map<String, Object>> list = new ArrayList<>();
        for (IiVideo video : videoList) {
            Map<String, Object> data = new HashMap<>();
            data.put("id", video.getId());
            data.put("cover", video.getCover());
            data.put("title", video.getTitle());
            IiAuthor author = iiService.getAuthorService().selectIiAuthorById(video.getAuthorId());
            data.put("author_name", author != null ? author.getName(): "");
            data.put("author_avatar", author != null ? author.getAvatar(): "");
            data.put("publish_time", dateTime.format(video.getPublishTime()));
            list.add(data);
        }
        return getDataTable(list);
    }

    @GetMapping({"/api/activity/coming/{page}"})
    @ResponseBody
    public TableDataInfo ActivityComingList(@PathVariable("page") Long page) {
        Date now = new Date();
        Long pageSize = Long.valueOf(9);
        List<IiActivity> activityList = iiService.getComingActivityList( dateTime.format(now), pageSize,(page - 1) * pageSize);
        List<Map<String, Object>> list = new ArrayList<>();
        for (IiActivity activity : activityList) {
            Map<String, Object> data = new HashMap<>();
            data.put("id", activity.getId());
            data.put("cover", activity.getCover());
            data.put("title", activity.getTitle());
            data.put("place", activity.getPlace());
            data.put("start_time", activity.getStartTime());
            list.add(data);
        }
        return getDataTable(list);
    }

    /*本周的查询*/
    @GetMapping({"/api/active/week/{page}"})
    @ResponseBody
    public TableDataInfo getWeek(@PathVariable("page") Integer page){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar cal = Calendar.getInstance();
        //设置一个星期的第一天，按中国的习惯一个星期的第一天是星期一
        cal.setFirstDayOfWeek(Calendar.MONDAY);
        //获得当前日期是一个星期的第几天
        int dayWeek = cal.get(Calendar.DAY_OF_WEEK);
        if(dayWeek==1){
            dayWeek = 8;
        }
        // 根据日历的规则，给当前日期减去星期几与一个星期第一天的差值
        cal.add(Calendar.DATE, cal.getFirstDayOfWeek() - dayWeek);
        Date mondayDate = cal.getTime();
        String weekBegin = sdf.format(mondayDate);

        cal.add(Calendar.DATE, 4 +cal.getFirstDayOfWeek());
        Date sundayDate = cal.getTime();
        String weekEnd = sdf.format(sundayDate);

        Integer pageSize  =Integer.valueOf(9);
        PageHelper.startPage(page,pageSize);
        List<IiActivity> iiActivities = iiService.getActivityService().selectWeek(weekBegin, weekEnd);
        List<Map<String, Object>> list = new ArrayList<>();
        for (IiActivity activity : iiActivities) {
            Map<String, Object> data = new HashMap<>();
            data.put("id", activity.getId());
            data.put("cover", activity.getCover());
            data.put("title", activity.getTitle());
            data.put("place", activity.getPlace());
            data.put("start_time", activity.getStartTime());
            list.add(data);
        }
        return getDataTable(list);

    }

    /*本月的查询*/
    @GetMapping({"/api/active/month/{page}"})
    @ResponseBody
    public TableDataInfo getMonth(@PathVariable("page") Integer page){
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.add(Calendar.MONTH, -1);
        calendar.set(Calendar.DAY_OF_MONTH, 0);

        // 获取当月第一天和最后一天
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String first, last;
        Calendar cale = Calendar.getInstance();

        // 获取前月的第一天
        cale = Calendar.getInstance();
        cale.add(Calendar.MONTH, 0);
        cale.set(Calendar.DAY_OF_MONTH, 1);
        first = format.format(cale.getTime());
        // 获取前月的最后一天
        cale = Calendar.getInstance();
        cale.add(Calendar.MONTH, 1);
        cale.set(Calendar.DAY_OF_MONTH, 0);
        last = format.format(cale.getTime());
        Integer pageSize  =Integer.valueOf(9);
        PageHelper.startPage(page,pageSize);
        List<IiActivity> iiActivities = iiService.getActivityService().selectMonth(first, last);

        List<Map<String, Object>> list = new ArrayList<>();
        for (IiActivity activity : iiActivities) {
            Map<String, Object> data = new HashMap<>();
            data.put("id", activity.getId());
            data.put("cover", activity.getCover());
            data.put("title", activity.getTitle());
            data.put("place", activity.getPlace());
            data.put("start_time", activity.getStartTime());
            list.add(data);
        }
        return getDataTable(list);

    }

    /*已结束*/
    @GetMapping({"/api/active/end/{page}"})
    @ResponseBody
    public TableDataInfo getActiveEnd(@PathVariable("page") Integer page){

        //得到long类型当前时间
        long l = System.currentTimeMillis();
        Date date = new Date(l);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String format = simpleDateFormat.format(date);

        Integer pageSize  =Integer.valueOf(9);
        PageHelper.startPage(page,pageSize);

        List<IiActivity> iiActivities = iiService.getActivityService().activeEnd(format);

        List<Map<String, Object>> list = new ArrayList<>();
        for (IiActivity activity : iiActivities) {
            Map<String, Object> data = new HashMap<>();
            data.put("id", activity.getId());
            data.put("cover", activity.getCover());
            data.put("title", activity.getTitle());
            data.put("place", activity.getPlace());
            data.put("start_time", activity.getStartTime());
            list.add(data);
        }
        return getDataTable(list);
    }

    /*未开始*/
    @GetMapping({"/api/active/start/{page}"})
    @ResponseBody
    public TableDataInfo getActiveStart(@PathVariable("page") Integer page){

        //得到long类型当前时间
        long l = System.currentTimeMillis();
        Date date = new Date(l);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String format = simpleDateFormat.format(date);

        Integer pageSize  =Integer.valueOf(9);
        PageHelper.startPage(page,pageSize);

        List<IiActivity> iiActivities = iiService.getActivityService().activeStart(format);

        List<Map<String, Object>> list = new ArrayList<>();
        for (IiActivity activity : iiActivities) {
            Map<String, Object> data = new HashMap<>();
            data.put("id", activity.getId());
            data.put("cover", activity.getCover());
            data.put("title", activity.getTitle());
            data.put("place", activity.getPlace());
            data.put("start_time", activity.getStartTime());
            list.add(data);
        }
        return getDataTable(list);
    }

    /*活动中*/
    @GetMapping({"/api/active/state/{page}"})
    @ResponseBody
    public TableDataInfo getActiveState(@PathVariable("page") Integer page){

        //得到long类型当前时间
        long l = System.currentTimeMillis();
        Date date = new Date(l);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String format = simpleDateFormat.format(date);
        Integer pageSize  =Integer.valueOf(9);
        PageHelper.startPage(page,pageSize);
        List<IiActivity> iiActivities = iiService.getActivityService().activeState(format);
        List<Map<String, Object>> list = new ArrayList<>();
        for (IiActivity activity : iiActivities) {
            Map<String, Object> data = new HashMap<>();
            data.put("id", activity.getId());
            data.put("cover", activity.getCover());
            data.put("title", activity.getTitle());
            data.put("place", activity.getPlace());
            data.put("start_time", activity.getStartTime());
            list.add(data);
        }
        return getDataTable(list);
    }


    //百科的模糊查询,已完成
    @GetMapping({"/api/encyclopedia/{name}/{page}"})
    @ResponseBody
    public TableDataInfo EncyclopediaByname(@PathVariable("name") String name,@PathVariable("page") Integer page){
        Integer pageSize  =Integer.valueOf(9);
        PageHelper.startPage(page,pageSize);
        List<IiEncyclopedia> encyclopediaList = iiService.getEncyclopediaService().findByKeyword(name);
        List<Map<String, Object>> list = new ArrayList<>();
        for (IiEncyclopedia encyclopedia : encyclopediaList) {
            Map<String, Object> data = new HashMap<>();
            data.put("id", encyclopedia.getId());
            data.put("name", encyclopedia.getName());
            data.put("description", encyclopedia.getDescription());
            list.add(data);
        }
        return getDataTable(list);
    }

    @GetMapping({"/api/encyclopedia/{page}"})
    @ResponseBody
    public TableDataInfo EncyclopediaList(@PathVariable("page") Long page) {
        Long pageSize = Long.valueOf(9);
        List<IiEncyclopedia> encyclopediaList = iiService.getEncyclopediaList(pageSize,(page - 1) * pageSize);
        List<Map<String, Object>> list = new ArrayList<>();
        for (IiEncyclopedia encyclopedia : encyclopediaList) {
            Map<String, Object> data = new HashMap<>();
            data.put("id", encyclopedia.getId());
            data.put("name", encyclopedia.getName());
            data.put("description", encyclopedia.getDescription());
            list.add(data);
        }
        return getDataTable(list);
    }

    @GetMapping({"/api/product/{page}"})
    @ResponseBody
    public TableDataInfo ProductList(@PathVariable("page") Long page) {
        Long pageSize = Long.valueOf(12);
        List<IiProduct> productList = iiService.getProductList(pageSize,(page - 1) * pageSize);
        List<Map<String, Object>> list = new ArrayList<>();
        for (IiProduct product : productList) {
            Map<String, Object> data = new HashMap<>();
            data.put("id", product.getId());
            data.put("logo", product.getLogo());
            data.put("name", product.getName());
            data.put("description", product.getDescription());
            list.add(data);
        }
        return getDataTable(list);
    }

    @GetMapping({"/api/solution/{page}"})
    @ResponseBody
    public TableDataInfo SolutionList(@PathVariable("page") Long page) {
        Long pageSize = Long.valueOf(9);
        List<IiSolution> solutionList = iiService.getSolutionList(pageSize,(page - 1) * pageSize);
        List<Map<String, Object>> list = new ArrayList<>();
        for (IiSolution solution : solutionList) {
            Map<String, Object> data = new HashMap<>();
            data.put("id", solution.getId());
            data.put("cover", solution.getCover());
            data.put("title", solution.getTitle());
            list.add(data);
        }
        return getDataTable(list);
    }

    @GetMapping({"/api/case/{page}"})
    @ResponseBody
    public TableDataInfo CaseList(@PathVariable("page") Long page) {
        Long pageSize = Long.valueOf(9);
        List<IiCase> caseList = iiService.getCaseList(pageSize,(page - 1) * pageSize);
        List<Map<String, Object>> list = new ArrayList<>();
        for (IiCase _case : caseList) {
            Map<String, Object> data = new HashMap<>();
            data.put("id", _case.getId());
            data.put("cover", _case.getCover());
            data.put("title", _case.getTitle());
            list.add(data);
        }
        return getDataTable(list);
    }

    @GetMapping({"/api/report/{page}"})
    @ResponseBody
    public TableDataInfo ReportList(@PathVariable("page") Long page) {
        Long pageSize = Long.valueOf(12);
        List<IiResearchReport> reportList = iiService.getResearchReportList(pageSize,(page - 1) * pageSize);
        List<Map<String, Object>> list = new ArrayList<>();
        for (IiResearchReport report : reportList) {
            Map<String, Object> data = new HashMap<>();
            data.put("id", report.getId());
            data.put("cover", report.getCover());
            data.put("title", report.getTitle());
            IiAuthor author = iiService.getAuthorService().selectIiAuthorById(report.getAuthorId());
            data.put("author_name", author != null ? author.getName(): "");
            data.put("author_avatar", author != null ? author.getAvatar(): "");
            list.add(data);
        }
        return getDataTable(list);
    }
}
